#include "SketcherHelper.h"
#include <math.h>


SketcherHelper* SketcherHelper::instance = 0;
void SketcherHelper::initializeInstance(int width, int height, Color* backColor, Color* borderColor){
	atexit(SDL_Quit );
	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
		cout << "Video initialization failed: %s\n"<<SDL_GetError( ) <<endl;
	}
	this->screen= SDL_SetVideoMode(width,height,COLOR_DEEP,SDL_HWSURFACE);
	if ( this->screen == NULL){
		cout <<  "error al inicializar el modo grafico: %s\n"<< SDL_GetError()<<endl;
	}

	SDL_WM_SetCaption("Zuma, the game","Zuma");

	Position p;
	p.setPixel(0,height/2);
	Uint32 rmask = 0xff000000;
	Uint32 gmask = 0x00ff0000;
	Uint32 bmask = 0x0000ff00;
	Uint32 amask = 0x00000000;
	
	if(this->background==NULL){
		SDL_FreeSurface(this->background);
	}
	this->background=SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,this->screen->format->BitsPerPixel,rmask,gmask,bmask,amask);

	drawSquare(&p,width,height,0,backColor,borderColor);

}

void SketcherHelper::initializeInstance(int width, int height, Texture* texture, Color* borderColor){
	atexit(SDL_Quit );
	if ( SDL_Init( SDL_INIT_VIDEO ) < 0 ){
		cout << "Video initialization failed: %s\n"<<SDL_GetError( ) <<endl;
	}
	this->screen= SDL_SetVideoMode(width,height,COLOR_DEEP,SDL_HWSURFACE);
	if ( this->screen==NULL){
		cout <<  "error al inicializar el modo grafico: %s\n"<< SDL_GetError()<<endl;
	}

	SDL_WM_SetCaption("Zuma, the game","Zuma");

	SDL_Rect dest;
	dest.w = width;
	dest.h = height;
	dest.x = 0;
	dest.y = 0;
	
	SDL_Surface *imagen;

	if(images[texture->getPath()]){
		imagen = images[texture->getPath().c_str()];
	}else{
		imagen = SDL_LoadBMP(texture->getPath().c_str());
		images[texture->getPath()] = imagen;
	}
	
	SDL_Surface * cuadrado=scaleSurface(imagen,width,height);
	if(borderColor != NULL){
		for(int i = 0 ; i < width; i++){
			Position p;
			p.setPixel(i,0);
			drawPixel(cuadrado,p,borderColor);
			p.setPixel(i,cuadrado->h-1);
			drawPixel(cuadrado,p,borderColor);
		}
		for(int i = 0 ; i < height; i++){
			Position p;
			p.setPixel(0,i);
			drawPixel(cuadrado,p,borderColor);
			p.setPixel(cuadrado->w-1,i);
			drawPixel(cuadrado,p,borderColor);
		}
	}
	if(this->background == NULL){
		SDL_FreeSurface(this->background);
	}
	Uint32 rmask = 0xff000000;
	Uint32 gmask = 0x00ff0000;
	Uint32 bmask = 0x0000ff00;
	Uint32 amask = 0x00000000;
	this->background=SDL_CreateRGBSurface(SDL_SWSURFACE,this->screen->w,this->screen->h,this->screen->format->BitsPerPixel,rmask,gmask,bmask,amask);

	SDL_BlitSurface(cuadrado,NULL,this->screen,&dest);
}

SketcherHelper* SketcherHelper::getInstance(){
	if(!instance){
		instance= new SketcherHelper();
	}
	return instance;
}
void SketcherHelper::endInitialicing(){
	for(int i = 0; i < this->background->w; i++){
		for(int j = 0; j < this->background->h; j++){
			Position p;
			p.setPixel(i,j);
			drawPixel(background,p,readPixel(this->screen,p));
		}
	}
}
void SketcherHelper::clearBackground(Position *p,int width,int height){
	for(int i = p->getPixelX()-1; i < p->getPixelX()+2 + width; i++){
		for(int j = p->getPixelY()-1; j < p->getPixelY()+2+height;j++){
			Position p;
			p.setPixel(i,j);
			if(p.getPixelX() >=0 &&p.getPixelY() >=0 &&p.getPixelX() < this->screen->w&&p.getPixelY()<this->screen->h)
				drawPixel(this->screen,p,readPixel(this->background,p));
		}
	}
}


void SketcherHelper::drawCircle(Position* center, int radiuos, Color* backColor, Color* borderColor)
{

	Uint32 rmask = 0xff000000;
	Uint32 gmask = 0x00ff0000;
	Uint32 bmask = 0x0000ff00;
	Uint32 amask = 0x00000000;
	SDL_Surface *back = SDL_CreateRGBSurface(SDL_SWSURFACE,1,1,this->screen->format->BitsPerPixel,amask,amask,amask,amask);
	SDL_Surface *border = SDL_CreateRGBSurface(SDL_SWSURFACE,1,1,this->screen->format->BitsPerPixel,amask,amask,amask,amask);

	Position pCero;
	pCero.setPixel(0,0);
	drawPixel(back,pCero,backColor);
	if(borderColor != NULL){
		drawPixel(border,pCero,borderColor);
	}
	double margin = radiuos * 0.04;

	for(int i = 0; i<2*(radiuos+margin);i++){
		for(int j = 0; j<2*(radiuos+margin);j++){
			Position p;
			p.setPixel(i+(center->getPixelX()-radiuos),j+(center->getPixelY()-radiuos));
			double longitud= calcDist(p,*center);
			if(radiuos+margin>=longitud && radiuos-margin<=longitud){
				if(borderColor!=NULL&&p.getPixelX()>=0&&p.getPixelY()>=0&&p.getPixelX()<this->screen->w&&p.getPixelY()<this->screen->h){
					drawPixel(this->screen,p,readPixel(border,pCero));
				}
			}else if(longitud<radiuos){
				if((p.getPixelX()>=0&&p.getPixelY()>=0&&p.getPixelX()<this->screen->w&&p.getPixelY()<this->screen->h)){
					drawPixel(this->screen,p,readPixel(back,pCero));	
				}
			}
		}
	}
}
void SketcherHelper::drawCircle(Position* center, int radiuos, Texture* backTexture, Color* borderColor){
	
    Uint32 rmask = 0xff000000;
	Uint32 gmask = 0x00ff0000;
	Uint32 bmask = 0x0000ff00;
	Uint32 amask = 0x00000000;
	SDL_Surface *border=SDL_CreateRGBSurface(SDL_SWSURFACE,1,1,this->screen->format->BitsPerPixel,amask,amask,amask,amask);

	Position pCeroBorder;
	pCeroBorder.setPixel(0,0);
	
	double margin=radiuos*0.04;
	if(borderColor==NULL){
		radiuos=radiuos+margin;
	}else{
		drawPixel(border,pCeroBorder,borderColor);
	}
	
	SDL_Surface *imagen;

	if(images[backTexture->getPath()]){
		imagen = images[backTexture->getPath().c_str()];
	}else{
		imagen = SDL_LoadBMP(backTexture->getPath().c_str());
		images[backTexture->getPath()] = imagen;
	}

	int width;
	int height;
	Position initial;
	if(imagen->w<imagen->h){
		width=2*radiuos+5;
		height=width*imagen->h/imagen->w+5;
		initial.setPixel(0,(height-2*radiuos)/2);
	}else{
		height=2*radiuos+5;
		width=height*imagen->w/imagen->h+5;
		initial.setPixel((width-2*radiuos)/2,0);
	}

	SDL_Surface * cuadrado=scaleSurface(imagen,width,height);

	Position pCero;
	for(int i=0;i<2*(radiuos+margin);i++){
		for(int j=0;j<2*(radiuos+margin);j++){
			Position p;
			p.setPixel(i+(center->getPixelX()-radiuos),j+(center->getPixelY()-radiuos));
			double longitud= calcDist(p,*center);
			if(radiuos+margin>=longitud && radiuos-margin<=longitud){
				if(borderColor!=NULL&&p.getPixelX()>=0&&p.getPixelY()>=0&&p.getPixelX()<this->screen->w&&p.getPixelY()<this->screen->h){
					drawPixel(this->screen,p,readPixel(border,pCeroBorder));
				}
			}else if(longitud<radiuos){
				pCero.setPixel(initial.getPixelX()+i,initial.getPixelY()+j);
				if((p.getPixelX()>=0&&p.getPixelY()>=0&&p.getPixelX()<this->screen->w&&p.getPixelY()<this->screen->h)&&
					pCero.getPixelX()>=0&&pCero.getPixelY()>=0&&pCero.getPixelX()<cuadrado->w&&pCero.getPixelY()<cuadrado->h){
					drawPixel(this->screen,p,readPixel(cuadrado,pCero));	
				}
			}
		}
	}
}


void SketcherHelper::drawSquareFromSurface(SDL_Surface* cuadrado,Position* position,double angle){

	Position* initialPosition=new Position();
	initialPosition->setPixel(position->getPixelX()+(cuadrado->h/2)*cos(pi*3/2+angle),
							  position->getPixelY()+(cuadrado->h/2)*sin(pi*3/2+angle));
	map<int,map<int,int> > alreadySettedPixels;
	Position p;
	Position pCero;
	if(sin(angle)==0 || cos(angle)==0){
		 for(int i=0;i<cuadrado->w;i++){
			for(int j=0;j<cuadrado->h;j++){
				p.setPixel(i+initialPosition->getPixelX(),j+initialPosition->getPixelY());
				pCero.setPixel(i,j);
				drawPixel(this->screen,p,readPixel(cuadrado,pCero));				
			}
		}
	 }else{

		 for(int i=initialPosition->getPixelX();i<cuadrado->w+initialPosition->getPixelX();i++){
			for(int j=initialPosition->getPixelY();j<cuadrado->h+initialPosition->getPixelY();j++){
				p.setPixel(i,j);
				double initialAlfa=calcAngle(*(initialPosition),p);
				double longitud=calcDist(*(initialPosition),p);
				Position rotated;
				rotated.setPixel(initialPosition->getPixelX()+longitud*cos(angle+initialAlfa),initialPosition->getPixelY()+longitud*sin(angle+initialAlfa));
				if(rotated.getPixelX() >= 0 && rotated.getPixelY() >= 0
							&&rotated.getPixelX() < this->screen->w
							&&rotated.getPixelY() < this->screen->h){
					pCero.setPixel(i-initialPosition->getPixelX(),j-initialPosition->getPixelY());
					drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
					alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]=1;
					//fila anterior
					rotated.decreasePixelX();
					if(rotated.getPixelX() >=0){
						rotated.increasePixelY();
						if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
							alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
							drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
						}
						rotated.decreasePixelY();
						if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
							alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
							drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
						}
						rotated.decreasePixelY();
						if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
							alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
							drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
						}
						rotated.increasePixelY();
					}
					rotated.increasePixelX();
					//fila actual
					rotated.increasePixelX();
					rotated.increasePixelY();
					if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
						alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
						drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
					}
					rotated.decreasePixelY();
					rotated.decreasePixelY();
					if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
						alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
						drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
					}
					rotated.increasePixelY();
					rotated.decreasePixelX();
					//fila siguiente
					rotated.increasePixelX();
					if(rotated.getPixelX() <this->screen->w){
						rotated.increasePixelY();
						if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
							alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
							drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
						}
						rotated.decreasePixelY();
						if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
							alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
							drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
						}
						rotated.decreasePixelY();
						if(rotated.getPixelY()<this->screen->h && rotated.getPixelY()>=0 &&
							alreadySettedPixels[rotated.getPixelX()][rotated.getPixelY()]==NULL){
							drawPixel(this->screen,rotated,readPixel(cuadrado,pCero));
						}
						rotated.increasePixelY();
					}
					rotated.decreasePixelX();								
				}
			}
		 }	
	 }
	 delete initialPosition;
}

void SketcherHelper::drawSquare(Position* position, int width, int height, double angle, Color* backColor, Color* borderColor)
{
	if(width==0 || height==0) return;
	Uint32 rmask=0xff000000;
	Uint32 gmask=0x00ff0000;
	Uint32 bmask=0x0000ff00;
	Uint32 amask=0x00000000;

	SDL_Surface *cuadrado=SDL_CreateRGBSurface(SDL_SWSURFACE,width,height,this->screen->format->BitsPerPixel,amask,amask,amask,amask);

	SDL_Rect dest;
	dest.w=width;
	dest.h=height;
	dest.x=0;
	dest.y=0;
	for(int i=0;i<cuadrado->w;i++){
		for(int j=0;j<cuadrado->h;j++){
			Position p;
			p.setPixel(i,j);
			drawPixel(cuadrado,p,backColor);
		}
	}
	//SDL_FillRect(cuadrado,&dest,SDL_MapRGB(this->screen->format,255,0,0));
	if(borderColor!=NULL){
		for(int i=0;i<width;i++){
			Position p;
			p.setPixel(i,0);
			drawPixel(cuadrado,p,borderColor);
			p.setPixel(i,cuadrado->h-1);
			drawPixel(cuadrado,p,borderColor);
		}
		for(int i=0;i<height;i++){
			Position p;
			p.setPixel(0,i);
			drawPixel(cuadrado,p,borderColor);
			p.setPixel(cuadrado->w-1,i);
			drawPixel(cuadrado,p,borderColor);
		}
	}
	drawSquareFromSurface(cuadrado,position,angle);
	
}

void SketcherHelper::drawSquare(Position* position, int width, int height, double angle, Texture* backTexture, Color* borderColor)
{	
	SDL_Rect dest;
	dest.w=width;
	dest.h=height;
	dest.x=0;
	dest.y=0;
	
	SDL_Surface *imagen;

	if(images[backTexture->getPath()]){
		imagen = images[backTexture->getPath().c_str()];
	}else{
		imagen = SDL_LoadBMP(backTexture->getPath().c_str());
		images[backTexture->getPath()] = imagen;
	}

	SDL_Surface * cuadrado=scaleSurface(imagen,width,height);
	if(borderColor!=NULL){
		for(int i=0;i<width;i++){
			Position p;
			p.setPixel(i,0);
			drawPixel(cuadrado,p,borderColor);
			p.setPixel(i,cuadrado->h-1);
			drawPixel(cuadrado,p,borderColor);
		}
		 for(int i=0;i<height;i++){
			Position p;
			p.setPixel(0,i);
			drawPixel(cuadrado,p,borderColor);
			p.setPixel(cuadrado->w-1,i);
			drawPixel(cuadrado,p,borderColor);
		}
	}
	drawSquareFromSurface(cuadrado,position,angle);
	
}



void SketcherHelper::drawLine(Position* fromPosition,Position* toPosition, Color* backColor, Color* borderColor){
	int b;
	float m;
	string s="bla";
	Position aux;
	aux.setPixel(toPosition->getPixelX()-fromPosition->getPixelX(),toPosition->getPixelY()-fromPosition->getPixelY());
	if(toPosition->getPixelX()-fromPosition->getPixelX()==0){
		for(int j=fromPosition->getPixelY(); j<=toPosition->getPixelY();j++){
			Position p;
			p.setPixel(toPosition->getPixelX(),j);
			drawPixel(this->screen,p,borderColor);
		}

		for(int j=fromPosition->getPixelY(); j>=toPosition->getPixelY();j--){
			Position p;
			p.setPixel(toPosition->getPixelX(),j);
			drawPixel(this->screen,p,borderColor);
		}
		return;
	}
	m=((float)aux.getPixelY())/((float)aux.getPixelX());
	b=fromPosition->getPixelY()-m*fromPosition->getPixelX();
	for(int i=fromPosition->getPixelX(); i>=toPosition->getPixelX();i--){
		for(int j=fromPosition->getPixelY(); j>=toPosition->getPixelY();j--){
			int Y=m*i+b;
			if(Y+1>=j&&Y-1<=j){
				Position p;
				p.setPixel(i,j);
				drawPixel(screen,p,borderColor);
			}
		}
	}
	for(int i=fromPosition->getPixelX(); i<= toPosition->getPixelX();i++){
		for(int j=fromPosition->getPixelY(); j<=toPosition->getPixelY();j++){
			int Y=m*i+b;
			if(Y+1>=j&&Y-1<=j){
				Position p;
				p.setPixel(i,j);
				drawPixel(screen,p,borderColor);
			}
		}
	}
}
SDL_Surface *SketcherHelper::scaleSurface(SDL_Surface *Surface, Uint16 Width, Uint16 Height)
{
	if(!Surface || !Width || !Height)
		return 0;

	SDL_Surface *_ret = SDL_CreateRGBSurface(Surface->flags, Width, Height, Surface->format->BitsPerPixel,
		Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);

	double  _stretch_factor_x = (static_cast<double>(Width)  / static_cast<double>(Surface->w)),
		_stretch_factor_y = (static_cast<double>(Height) / static_cast<double>(Surface->h));

	for(Sint32 y = 0; y < Surface->h; y++)
		for(Sint32 x = 0; x < Surface->w; x++)
			for(Sint32 o_y = 0; o_y < _stretch_factor_y; ++o_y)
				for(Sint32 o_x = 0; o_x < _stretch_factor_x; ++o_x){
					Position aux;
					aux.setPixel(static_cast<Sint32>(_stretch_factor_x * x) + o_x,
						         static_cast<Sint32>(_stretch_factor_y * y) + o_y);
					Position initial;
					initial.setPixel(x,y);
					drawPixel(_ret,aux,readPixel(Surface,initial));
				}

	return _ret;
}
double SketcherHelper::calcDist(Position from,Position to){
	double distx=to.getPixelX()-from.getPixelX();
	double disty=to.getPixelY()-from.getPixelY();
	return pow( pow (distx, 2)+pow (disty, 2),0.5);
}
double SketcherHelper::calcAngle(Position from,Position to){
	double distx=to.getPixelX()-from.getPixelX();
	double disty=to.getPixelY()-from.getPixelY();
	return (double)atan2(disty,distx);
}	


void SketcherHelper::drawPixel(SDL_Surface* target, Position position, Uint32 pixel){
	int x=position.getPixelX();
	int y=position.getPixelY();
	int bpp = target->format->BytesPerPixel;
	/* Here p is the address to the pixel we want to set */
	Uint8 *p = (Uint8 *)target->pixels + y * target->pitch + x * bpp;

	switch(bpp) {
	case 1:
		*p = pixel;
		break;

	case 2:
		*(Uint16 *)p = pixel;
		break;

	case 3:
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
			p[0] = (pixel >> 16) & 0xff;
			p[1] = (pixel >> 8) & 0xff;
			p[2] = pixel & 0xff;
		} else {
			p[0] = pixel & 0xff;
			p[1] = (pixel >> 8) & 0xff;
			p[2] = (pixel >> 16) & 0xff;
		}
		break;

	case 4:
		*(Uint32 *)p = pixel;
		break;
	}

} 

Uint32  SketcherHelper::readPixel(SDL_Surface* source,  Position position){
	int x=position.getPixelX();
	int y=position.getPixelY();
	int bpp = source->format->BytesPerPixel;
	/* Here p is the address to the pixel we want to retrieve */
	Uint8 *p = (Uint8 *)source->pixels + y * source->pitch + x * bpp;

	switch(bpp) {
	case 1:
		return *p;
		break;

	case 2:
		return *(Uint16 *)p;
		break;

	case 3:
		if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
			return p[0] << 16 | p[1] << 8 | p[2];
		else
			return p[0] | p[1] << 8 | p[2] << 16;
		break;

	case 4:
		return *(Uint32 *)p;
		break;

	default:
		return 0;       /* shouldn't happen, but avoids warnings */
	}

} 
void SketcherHelper::drawPixel(SDL_Surface *target, Position position, Color * color){
  int x=position.getPixelX();
  int y=position.getPixelY();
  Uint32 c = SDL_MapRGB(target->format, color->getR(), color->getG(), color->getB());
  switch (target->format->BytesPerPixel)
  {
    case 1: // 8-bpp
      {
        Uint8 *bufp;
        bufp = (Uint8 *)target->pixels + y*target->pitch + x;
        *bufp = c;
      }
      break;
    case 2: // 15-bpp or 16-bpp
      {
        Uint16 *bufp;
        bufp = (Uint16 *)target->pixels + y*target->pitch/2 + x;
        *bufp = c;
      }
      break;
    case 3: // 24-bpp mode, usually not used
      {
        Uint8 *bufp;
        bufp = (Uint8 *)target->pixels + y*target->pitch + x * 3;
        if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
        {
          bufp[0] = c;
          bufp[1] = c >> 8;
          bufp[2] = c >> 16;
        } else {
          bufp[2] = c;
          bufp[1] = c >> 8;
          bufp[0] = c >> 16;
        }
      }
      break;
    case 4: // 32-bpp
      {
        Uint32 *bufp;
        bufp = (Uint32 *)target->pixels + y*target->pitch/4 + x;
        *bufp = c;
      }
      break;
  }
} 

void SketcherHelper::show(){
	SDL_Flip(this->screen);
}

void SketcherHelper::setCoefs(float coeficienteX, float coeficienteY, float coeficiente, int initialX, int initialY, int pixelsHeight) {
	this->coeficienteX = coeficienteX;
	this->coeficienteY = coeficienteY;
	this->coeficiente = coeficiente;
	this->initialX = initialX;
	this->initialY = initialY;
	this->pixelsHeight = pixelsHeight;
}

Position* SketcherHelper::getPositionInPixels( Position * position ) {
	return new Position(position->getAxisX()*coeficiente-initialX,pixelsHeight - (position->getAxisY()*coeficiente-initialY));  
}

Position* SketcherHelper::getPositionMeasurementUnit( Position * position ){
	return new Position((position->getAxisX()+initialX)/this->coeficiente,(pixelsHeight - position->getAxisY() + initialY) / coeficiente);
}

void SketcherHelper::drawTriangle( TriangularObstacle * triangle, Color * backGroundColor, Color * lineColor ){
	Position pixelToDraw;
	pixelToDraw.setPixel(0,0);
	list<Position*>::iterator iterEdge1;
	int i = 0;

	// Define el minimo rectangulo para copiar pixeles.
	int minPixelX = min(triangle->getP1()->getPixelX(),triangle->getP2()->getPixelX());
	minPixelX = min(minPixelX, triangle->getP3()->getPixelX());
	int minPixelY = min(triangle->getP1()->getPixelY(),triangle->getP2()->getPixelY());
	minPixelY = min(minPixelY, triangle->getP3()->getPixelY());	
	int maxPixelX = max(triangle->getP1()->getPixelX(),triangle->getP2()->getPixelX());
	maxPixelX = max(maxPixelX, triangle->getP3()->getPixelX());
	int maxPixelY = max(triangle->getP1()->getPixelY(),triangle->getP2()->getPixelY());
	maxPixelY = max(maxPixelY, triangle->getP3()->getPixelY());		

	for(int i = minPixelX ; i<= maxPixelX ; i++) {
		for(int j = minPixelY ; j<= maxPixelY ; j++) {
			Position * p = new Position();
			p->setPixel(i,j);
			int included = triangle->include(p);
			switch (included) {
				case 0:
					drawPixel(screen,*p,lineColor);
					printf("Borde: (%i,%i)\n" ,i,j);
					break;
				case 1:
					drawPixel(screen,*p,backGroundColor);
					printf("Interno: (%i,%i)\n" ,i,j);
					break;
			}
		}
	}



}